package com.basker.pisces.domain.entity.event.subcriber;

import com.basker.pisces.domain.entity.event.DataObjectCreateEvent;
import com.basker.pisces.domain.entity.event.IDataEntitySubcriber;
import com.basker.pisces.domain.entity.meta.Entity;
import com.basker.pisces.domain.property.meta.Property;
import com.basker.pisces.domain.property.meta.PropertyCollection;

/**
 * 初始化值处理器
 *
 * @author hangwen
 */
public class InitialValueHanlder implements IDataEntitySubcriber {

    @Override
    public void onDataObjectCreated(DataObjectCreateEvent event) {
        Object dataObject = event.getDataObject();
        if (dataObject == null) {
            return;
        }

        Entity entity = event.getRootEntity();
        PropertyCollection properties = entity.getProperties();

        for (Property<?> property : properties) {
            Object initialValue = property.getInitialValue();
            if (initialValue == null) {
                continue;
            }

            property.setValue(dataObject, initialValue);
        }
    }
}
